package com.dhecp.project.purchase.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.purchase.domain.PurchasingSupplier;
import com.dhecp.project.purchase.mapper.PurchasingSupplierMapper;
import com.dhecp.project.purchase.service.PurchasingSupplierService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 采购供应商备案
 * @author: hlf
 * @create: 2022-10-28 16:43
 */
@Service
public class PurchasingSupplierServiceImpl implements PurchasingSupplierService {

    @Autowired
    private PurchasingSupplierMapper dataInfoMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**
     * 查询
     * @param params
     * @return List - PurchasingSupplier
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<PurchasingSupplier> queryData(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));		//数据权限查询条件SQL语句
        PageUtils.startPage();
        return dataInfoMapper.findData(params);
    }

    /**
     * 通过guid查询
     * @param guid 必要参数：行唯一值
     * @return PurchasingSupplier 实体
     * @author DH-Xxj
     */
    @Override
    public PurchasingSupplier queryDataByGuid(Map<String, Object> params) {
        return dataInfoMapper.findDataByGuid(params);
    }

    /**
     * 新增
     * @param infoItems 必要参数：对应实体类的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Override
    public AjaxResult insertData(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        List<PurchasingSupplier> dataInfoList = JSON.parseArray(params.get("infoItems").toString(), PurchasingSupplier.class);

        if (dataInfoList == null || dataInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (PurchasingSupplier dataInfo : dataInfoList) {
            dataInfo.setCreateTime(new Date());
            dataInfo.setUpdateTime(new Date());
            dataInfo.setcUser(ShiroUtils.getUserId());
            dataInfo.setcUserName(ShiroUtils.getUserName());
            dataInfo.setcDept(ShiroUtils.getUserDeptPath());
            dataInfo.setcDeptName(ShiroUtils.getUserDeptName());
            dataInfo.setcPosition(ShiroUtils.getUserPositionName());

            dataInfoMapper.insertData(dataInfo);
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 删除
     * @param infoItems 必要参数：对应实体类的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Override
    public AjaxResult deleteData(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        List<PurchasingSupplier> dataInfoList = JSON.parseArray(params.get("infoItems").toString(), PurchasingSupplier.class);
        if (dataInfoList == null || dataInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (PurchasingSupplier dataInfo : dataInfoList) {
            dataInfoMapper.deleteData(dataInfo);
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 通过guid修改数据
     * @param infoItems 必要参数：对应实体类的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Override
    public AjaxResult updateByGuid(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        List<PurchasingSupplier> dataInfoList = JSON.parseArray(params.get("infoItems").toString(), PurchasingSupplier.class);
        if (dataInfoList == null || dataInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (PurchasingSupplier dataInfo : dataInfoList) {
            dataInfo.setUpdateTime(new Date());
            dataInfoMapper.updateByGuid(dataInfo);
        }
        return AjaxResult.success("操作成功");
    }

}